package com.inspinia.message.common.model;

import cn.hutool.json.JSONUtil;
import com.inspinia.base.util.JsonUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/**
 * 发消息方式 抽象出来
 * @author Veblen
 */
public abstract class BaseWebSocketServerHandler extends SimpleChannelInboundHandler<Object> {


    /**
     * 推送单个notice
     */
    public static final void push(final ChannelHandlerContext ctx, final NoticeLinkType type) {
        Notice notice = new Notice();
        notice.setLinkType(NoticeLinkType.sys);
        TextWebSocketFrame tws = new TextWebSocketFrame(JsonUtils.toJson(notice));
        ctx.channel().writeAndFlush(tws);
    }

    /**
     * 推送单个文本
     */
    public static final void push(final ChannelHandlerContext ctx, final String message) {
        TextWebSocketFrame tws = new TextWebSocketFrame(message);
        ctx.channel().writeAndFlush(tws);
    }

    /**
     * 推送群发文本
     */
    public static final void push(final ChannelGroup ctxGroup, final String message) {

        TextWebSocketFrame tws = new TextWebSocketFrame(message);
        ctxGroup.writeAndFlush(tws);
    }

    /**
     * 群发推送noticeLinkType
     */
    public static final void push(final ChannelGroup ctxGroup, final NoticeLinkType type) {

        TextWebSocketFrame tws = new TextWebSocketFrame(JSONUtil.toJsonStr(type));
        ctxGroup.writeAndFlush(tws);

    }
}
